home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROCS.ZIP / VARSUB.ICN < prev    next >
Text File  |  1992-09-28  |  2KB  |  69 lines

  1. ############################################################################
  2. #
  3. #    File:     varsub.icn
  4. #
  5. #    Subject:  Procedures to perform UNIX-shell-style variable substitution
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     May 11, 1992
  10. #
  11. ###########################################################################
  12. #
  13. #  Variable values are obtained from the supplied procedure, "varProc",
  14. #  which returns the value of its variable-name argument or fails if
  15. #  there is no such variable.  "varProc" defaults to the Icon built-in
  16. #  function, getenv.
  17. #
  18. #  As with the UNIC Bourne shell and C shell, variable names are
  19. #  preceded by $.  Optionally, the variable name can additionally be
  20. #  surrounded by curly braces {}, which is usually done when necessary
  21. #  to isolate the variable name from surrounding text.
  22. #
  23. #  As with the C-shell, the special symbol ~<username> is handled.
  24. #  Username can be omitted, in which case the value of the variable
  25. #  "HOME" is substituted.  If username is supplied, the /etc/passwd file
  26. #  is searched to supply the home directory of username (this action is
  27. #  obviously not portable to non-UNIX environments).
  28. #
  29. ############################################################################
  30.  
  31. procedure varsub(s,varProc)
  32.    local var, p, user, pw, i, line
  33.    static nameChar
  34.    initial nameChar := &letters ++ &digits ++ "_"
  35.    /varProc := getenv
  36.    s ? {
  37.       s := ""
  38.       while s ||:= tab(upto('$~')) do {
  39.      p := &pos
  40.      s ||:= case move(1) of {
  41.         "$": {
  42.            if tab(any('{(')) then var := tab(upto('})')) & move(1)
  43.            else var := tab(many(nameChar)) | ""
  44.            "" ~== varProc(var) | &subject[p:&pos]
  45.            }
  46.         "~": {
  47.            if user := tab(many(nameChar)) || ":" then {
  48.           if pw := open("/etc/passwd") then {
  49.              (while line := read(pw) do 
  50.                if match(user,line) then break) | (line := &null)
  51.              close(pw)
  52.              if \line then {
  53.             every i := find(":",line)\5
  54.             i +:= 1
  55.             line[i:find(":",line,i)]
  56.             }
  57.              else &subject[p:&pos]
  58.              }
  59.           else &subject[p:&pos]
  60.           }
  61.            else getenv("HOME") 
  62.            }
  63.         }
  64.      }
  65.       s ||:= tab(0)
  66.       }
  67.    return s
  68. end
  69.